<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;
use App\AccountLog;
use app\common\model\AppAccountLog;
use app\common\model\ApplicationOrder;
use app\common\model\ApplicationClass;
use think\Db;

/**
 * 配置控制器
 */
class Application extends AdminBase
{

    public function ApplicationList()
    {

        $where = $this->logicApplication->getWhere($this->param);

        $this->assign('list', $this->logicApplication->getApplicationList($where, 'a.*', 'a.create_time asc'));

        return $this->fetch('application_list');
    }

    /**
     * Application添加
     */
    public function ApplicationAdd()
    {

        $this->ApplicationCommon();



        $this->assign('class',$this->logicApplication->getApplicationClassList()['list']);
        return $this->fetch('application_edit');
    }

    /**
     * Application编辑
     */
    public function ApplicationEdit()
    {

        $this->ApplicationCommon();

        $info = $this->logicApplication->getApplicationInfo(['a.id' => $this->param['id']], 'a.*');


        $this->assign('info', $info);
        $this->assign('class',$this->logicApplication->getApplicationClassList()['list']);
        return $this->fetch('application_edit');
    }

    /**
     * Application添加与编辑通用方法
     */
    public function ApplicationCommon()
    {

        IS_POST && $this->jump($this->logicApplication->ApplicationEdit($_POST));
    }



    public function applicationSetStatus(){
        $id = $this->param['id'];
        $status = $this->param['status'];
        $res = Db::name('application')->where(['id'=>$id])->update(['status'=>$status]);
        if(!$res){
            $this->error('修改失败');
        }else{
            $this->success('修改成功');
        }
    }

    public function applicationClass(){
        $list = Db::name('application_class')->where('1=1')->select();

        $this->assign('list',$list);
        return $this->fetch('application/application_class');
    }

    public function applicationClassAdd(){
     if(IS_POST){
        $data = array(
            'name' => $this->param['name'],
            'sort'=> $this->param['sort'],
            'status'=>1,
            'update_time'=>time(),
        );
        if($this->param['id']==0){
            $data['create_time']=time();
            $res = Db::name("application_class")->insert($data);
            if(!$res){
                $this->error('添加失败');
            }else{
                $this->success('添加成功');
            }
        }else{
            $res = Db::name("application_class")->where(['id'=>$this->param['id']])->update($data);
            if(!$res){
                $this->error('修改失败');
            }else{
                $this->success('修改成功');
            }
        }
    }
        if($this->param['id']!=0){
            $info = Db::name('application_class')->where(['id'=>$this->param['id']])->find();

            $this->assign('info',$info);
        }
        return $this->fetch('application/application_class_edit');
    }

    public function applicationClassSetStatus(){
        $id = $this->param['id'];
        $status = $this->param['status'];
        $res = Db::name('application_class')->where(['id'=>$id])->update(['status'=>$status]);
        if(!$res){
            $this->error('修改失败');
        }else{
            $this->success('修改成功');
        }
    }


    public function applicationOrder(){
        $where=[];
        !empty($this->param['search_data']) && $where['c.username|c.mobile']=$this->param['search_data'];
        !empty($this->param['app_id']) && $where['a.app_id']=$this->param['app_id'];
        if(isset($this->param['status']) && $this->param['status']=='0'){
            $where['a.status']=$this->param['status'];
        }else{
            !empty($this->param['status']) && $where['a.status']=$this->param['status'];
        }
        if (!empty($this->param['date1']) && !empty($this->param['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($this->param['date1'] . ' 00:00:00'), strtotime($this->param['date2'] . ' 23:59:59')]];
        }
        $list = ApplicationOrder::alias("a")->field('a.*,c.username,c.is_company,c.mobile')->join('__MEMBER__ c','a.user_id=c.id')->where($where)->order('a.id desc')->paginate(10,false,['query'=>$this->param]);
        $count=ApplicationOrder::alias("a")->join('__MEMBER__ c','a.user_id=c.id')->where($where)->count();
        $this->assign('list',['list'=>$list,'count'=>$count]);
        $this->assign('param',$this->param);
        $this->assign('app_list',\app\common\model\Application::all(['status'=>1]));
        return $this->fetch('application/application_order');
    }

    public function membercard(){
        $list = Db::name("application_membercard")->alias("a")->join('__APPLICATION__ b','a.app_id=b.id')->field("a.*,b.name as app_name")->where('1=1')->select();


        $this->assign('list',$list);

        return $this->fetch('application/application_membercard');
    }

    //拒绝发货
    public function applicationOrderRefuse(){
        $id = $this->param['id'];
        $res = Db::name('application_order')->where(['id'=>$id])->update(['status'=>-1,'end_time'=>time()]);
        if(!$res){
            $this->error('修改失败');
        }else{
            $this->success('修改成功');
        }
    }

    //确认发货
    public function applicationOrderConfirm(){

        if(IS_POST){
            $info= Db::name('application_order')->where(['id'=>$this->param['id']])->find();
            if($this->param['status']==-1){
                if(empty($this->param['password'])){
                    $this->error('取消原因不能为空');
                }
                if($info['status']==1){
                    //退回余额
                    $member=\app\common\model\Member::get($info['user_id']);
                    $member->app_balance-=$info['price'];
                    $member->save();
                    AppAccountLog::create([
                        "user_id"=>$info['user_id'],
                        "type"=>5,
                        'is_add'=>1,
                        'remark'=>'取消退回',
                        'number'=> $info['price']
                    ]);

                }
                $res = Db::name('application_order')->where(['id'=>$this->param['id']])->update(['status'=>$this->param['status'],'remarks'=>$this->param['password'],'end_time'=>time()]);
            }else{
                if($info['type']==1 || $info['type']==2){
                    if(empty($this->param['password'])){
                        $this->error('卡密不能为空');
                    }
                    if(Db::name('application_user_ticket')->where(['password'=>$this->param['password']])->count()){
                        $this->error('卡密已使用');
                    }
                    $ticket= Db::name('application_ticket')->where(['id'=>$info['ticket_id']])->find();
                    Db::name('application_user_ticket')->insert([
                        'account'=>$info['account'],
                        'user_id'=>$info['user_id'],
                        'app_id'=>$info['app_id'],
                        'price'=>$info['price'],
                        'password'=>$this->param['password'],
                        'amount'=>$ticket['amount'],
                        'status'=>1,
                        'create_time'=>TIME_NOW,
                        'expire_time'=>TIME_NOW+$info['period']*86400
                    ]);
                }else{

                    Db::name('application_user_ticket')->insert([
                        'user_id'=>$info['user_id'],
                        'app_id'=>$info['app_id'],
                        'price'=>$info['price'],
//                        'password'=>$this->param['password'],
                        'amount'=>$info['price'],
                        'account'=>$info['account'],
                        'card_type'=>$info['card_type'],
                        'month'=>$info['month'],
                        'status'=>1,
                        'create_time'=>TIME_NOW,
                        'expire_time'=>strtotime("+ ".$info['month']." month")
                    ]);
                }
                if(!empty($this->param['password'])){
                    $res = Db::name('application_order')->where(['id'=>$this->param['id']])->update(['status'=>$this->param['status'],'password'=>$this->param['password'],'end_time'=>time()]);
                }else{
                    $res = Db::name('application_order')->where(['id'=>$this->param['id']])->update(['status'=>$this->param['status'],'end_time'=>time()]);
                }

            }

            if(!$res){
                $this->error('修改失败');
            }else{
                $this->success('修改成功');
            }
        }else{
            $this->assign('param',$this->param);
            return $this->fetch('application_order_confirm');
        }

    }

    //券分类
    public function applicationTicket(){
        $list = Db::name("application_ticket")->alias('a')->field('a.*,b.name')->join('__APPLICATION__ b', 'a.app_id = b.id')->where(['a.status'=>['in',[0,1]]])->select();

        $this->assign('list',$list);
        return $this->fetch('application_ticket');
    }

    //增加券分类
    public function applicationTicketAdd(){
        $applications = DB::name('application')->where(['status'=>1])->select();
        $this->assign('applications',$applications);
        if(IS_POST){
            $data = array(
                'app_id' => $this->param['app_id'],
                'price'=> $this->param['price'],
                'title'=> $this->param['title'],
                'amount'=> $this->param['amount'],
                'period'=> $this->param['period'],
                'status'=>1,
                'update_time'=>time(),
            );
            if($this->param['id']==0){
                $data['create_time']=time();
                $res = Db::name("application_ticket")->insert($data);
                if(!$res){
                    $this->error('添加失败');
                }else{
                    $this->success('添加成功');
                }
            }else{
                $res = Db::name("application_ticket")->where(['id'=>$this->param['id']])->update($data);
                if(!$res){
                    $this->error('修改失败');
                }else{
                    $this->success('修改成功');
                }
            }
        }
        if($this->param['id']!=0){
            $info = Db::name('application_ticket')->where(['id'=>$this->param['id']])->find();

            $this->assign('info',$info);
        }
        return $this->fetch('application/application_ticket_edit');
    }

    /**
     * 数据状态设置
     */
    public function ticketSetStatus()
    {

        $this->jump($this->logicAdminBase->setStatus('ApplicationTicket', $this->param));
    }

    //增加券分类
    public function applicationmembercardAdd(){
        $applications = DB::name('application')->where(['status'=>1])->select();
        $this->assign('applications',$applications);
        if(IS_POST){
            $data = array(
                'app_id' => $this->param['app_id'],
                'name'=> $this->param['name'],
                'describer'=> $this->param['describer'],
                'type'=>$this->param['type'],
                'level'=>$this->param['level'],
                'official_price'=>$this->param['official_price'],
                'real_price'=>$this->param['real_price'],
                'status'=>1,
                'update_time'=>time(),
            );
            if($this->param['id']==0){
                $data['create_time']=time();
                $res = Db::name("application_membercard")->insert($data);
                if(!$res){
                    $this->error('添加失败');
                }else{
                    $this->success('添加成功');
                }
            }else{
                $res = Db::name("application_membercard")->where(['id'=>$this->param['id']])->update($data);
                if(!$res){
                    $this->error('修改失败');
                }else{
                    $this->success('修改成功');
                }
            }
        }
        if($this->param['id']!=0){
            $info = Db::name('application_membercard')->where(['id'=>$this->param['id']])->find();

            $this->assign('info',$info);
        }
        return $this->fetch('application/application_membercard_edit');
    }
     public function applicationmembercardsetstatus(){
        $this->jump($this->logicAdminBase->setStatus('ApplicationMembercard', $this->param));
     }


}
